package cn.bugstack.domain.activity.service;

import cn.bugstack.domain.activity.adapter.repository.IActivityRepository;
import cn.bugstack.domain.activity.model.entity.MarketProductEntity;
import cn.bugstack.domain.activity.model.entity.TrialBalanceEntity;
import cn.bugstack.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import cn.bugstack.domain.activity.model.valobj.TeamStatisticVO;
import cn.bugstack.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.bugstack.types.design.framework.tree.StrategyHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService{
    @Resource
    private DefaultActivityStrategyFactory defaultActivityStrategyFactory;

    @Resource
    private IActivityRepository activityRepository;

    /**
     * @description: 主页的优惠试算
     * @author: 刘新智
     * @date: 2025/10/28 16:37
     * @param: [marketProductEntity]
     * @return: cn.bugstack.domain.activity.model.entity.TrialBalanceEntity
     **/
    @Override
    public TrialBalanceEntity indexMarketTrail(MarketProductEntity marketProductEntity) throws Exception {
        StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> strategyHandler = defaultActivityStrategyFactory.strategyHandler();
        return strategyHandler.apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
    }

    @Override
    public List<UserGroupBuyOrderDetailEntity> queryProgressUserGroupBuyOrderDetailList(Long activityId, String userId, int ownerCount, int randomCount) {
        List<UserGroupBuyOrderDetailEntity> userGroupBuyOrderDetailEntityList = new ArrayList<>();
        if(0 != ownerCount){
            List<UserGroupBuyOrderDetailEntity> ownerList= activityRepository.queryProgressUserGroupBuyOrderDetailListByOwner(activityId, userId, ownerCount);
            if(null != ownerList && !ownerList.isEmpty()){
                userGroupBuyOrderDetailEntityList.addAll(ownerList);
            }
        }
        if(0 != randomCount){
            List<UserGroupBuyOrderDetailEntity> randomList= activityRepository.queryProgressUserGroupBuyOrderDetailListByRandom(activityId, userId, randomCount);
            if(null != randomList && !randomList.isEmpty()){
                userGroupBuyOrderDetailEntityList.addAll(randomList);
            }
        }
        return userGroupBuyOrderDetailEntityList;
    }

    @Override
    public TeamStatisticVO queryTeamStatisticVO(Long activityId) {
        return activityRepository.queryTeamStatisticVO(activityId);
    }
}
